/***********************************************************************************************************************
/*
/*    cVersion.h
/*
/**********************************************************************************************************************/

#pragma once

#include "tStr.h"
#include <regex>


//----------------------------------------------------------------------------------------------------------------------
// CLASS VERSION
//----------------------------------------------------------------------------------------------------------------------
class cVersion
{
	public:

		//-------------------------------------------
		// CONSTRUCTORS
		//-------------------------------------------
		cVersion ( unsigned M, unsigned m, unsigned p, unsigned b ) : uMaj(M), uMin(m), uPip(p), uBld(b) {}
		cVersion () : uMaj(0),uMin(0),uPip(0), uBld(0) {}

		//-------------------------------------------
		// GET BUILD
		//-------------------------------------------
		unsigned build () { return uBld; }
		
		//-------------------------------------------
		// OPERATOR EQUAL
		//-------------------------------------------
		bool operator== ( const cVersion & r )
		{
			return ( uMaj == r.uMaj &&
				     uMin == r.uMin &&
					 uPip == r.uPip &&
					 uBld == r.uBld );
		}

		//-------------------------------------------
		// OPERATOR UNEQUAL
		//-------------------------------------------
		bool operator!= ( const cVersion & rh ) { return ! ( *this == rh ); };

		//-------------------------------------------
		// GET TEXT
		// i.e. "0.1.29 build 104";
		//-------------------------------------------
		wchar_t * get_text ( cStrW * pRet ) const  	
		{
			assert ( pRet );
			wchar_t t [1000];
			swprintf ( t, sizeof(t)/sizeof(wchar_t), L"%u.%u.%u build %u", uMaj, uMin, uPip, uBld );
			*pRet = t;
			return pRet->cstr();
		}

		//-------------------------------------------
		// SET TEXT
		// i.e. "0.1.29 build 104"
		//-------------------------------------------
		bool set_from_text ( const wchar_t * pSig )
		{
			std::tr1::wregex  rgx ( L"(\\d+).(\\d+).(\\d+) build (\\d+)" );
			std::tr1::wcmatch bmatch;

			if ( std::tr1::regex_match ( pSig, bmatch, rgx ) )
			{
				uMaj = _wtoi ( static_cast<std::wstring>(bmatch[1]).c_str() );
				uMin = _wtoi ( static_cast<std::wstring>(bmatch[2]).c_str() );
				uPip = _wtoi ( static_cast<std::wstring>(bmatch[3]).c_str() );
				uBld = _wtoi ( static_cast<std::wstring>(bmatch[4]).c_str() );

				return true;
			}
			return false;
		}

		//-------------------------------------------
		// DATA
		//-------------------------------------------
		unsigned uMaj;
		unsigned uMin;
		unsigned uPip;
		unsigned uBld;

};


